package rkdwogus7.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Card_Layout extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final CardLayout cards = new CardLayout(0, 0);  // 1
	private JPanel cardpanel = new JPanel();  // 여기에 선언

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Card_Layout dialog = new Card_Layout();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Card_Layout() {
		setTitle(":: Card Layout ::");
		setBounds(100, 100,640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			// JPanel cardpanel = new JPanel();  // 위에 다시 선언했어
			contentPanel.add(cardpanel, BorderLayout.CENTER);
			cardpanel.setLayout( cards );  // 2
			// CardLayout이 적용된 Panel에 2개의 Panel(login, logout)을 카드처럼 포개어 놓음
			// 지정한 Panel을 언제든지 보여주기 위해 고유한 이름을 각각 부여 3a, 3b
			// show method로 login, logout을 부를 수 있음
			{
				JPanel login = new JPanel();
				login.setBackground(Color.YELLOW);
				cardpanel.add(login, "login");  // 3a
				// 카드 레이아웃에 부여된 고유 이름을 바꾸어야 우리가 편집할 수 있음
			}
			{
				JPanel logout = new JPanel();
				logout.setBackground(Color.ORANGE);
				cardpanel.add(logout, "logout");  // 3b
			}
		}
		{
			final JPanel btnpanel = new JPanel();  // 여기 final은 왜 준거야?
			contentPanel.add(btnpanel, BorderLayout.SOUTH);
			{
				JButton loginBtn = new JButton(" 로그인");
				loginBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cards.show(cardpanel, "login");  // 4a
						// cards.next(cardpanel);  // 4b
					}
				});
				btnpanel.add(loginBtn);
			}
			{
				JButton LogoutBtn = new JButton(" 로그아웃");
				LogoutBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cards.show(cardpanel, "logout");  // 4a
						// cards.last(cardpanel);  // 4b
					}
				});
				btnpanel.add(LogoutBtn);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("OK");
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
	}

}
